import java.util.*;


public class Leetcode1224 {
    public static void main(String[] args) {
        System.out.println(maxEqualFreq(new int[]{1, 1, 1, 2, 2, 2, 3, 3, 3}));//7
        System.out.println(maxEqualFreq(new int[]{1, 2, 3, 1, 2, 3, 4, 4, 4, 4, 1, 2, 3, 5, 6}));//7
        System.out.println(maxEqualFreq(new int[]{10, 2, 8, 9, 3, 8, 1, 5, 2, 3, 7, 6}));//5
        System.out.println(maxEqualFreq(new int[]{1, 1, 1, 2, 2, 2}));//5
        System.out.println(maxEqualFreq(new int[]{2, 2, 1, 1, 5, 3, 3, 5}));//7
        System.out.println(maxEqualFreq(new int[]{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5}));//13
        System.out.println(maxEqualFreq(new int[]{1, 1, 1, 1, 1, 1}));//6
    }

    /*
    2 1
    3 1
     */
    public static int maxEqualFreq(int[] nums) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);

            if (map.size() == 1) {
                res = i + 1;
            } else {
                Set<Integer> set = new TreeSet<>(map.values());
                if (set.size() == 1 && set.contains(1)) {
                    res = Math.max(res, i + 1);
                    continue;
                }

                if (set.size() != 2) {
                    continue;
                }

                Iterator<Integer> iterator = set.iterator();
                int left = iterator.next();//1
                int right = iterator.next();//2

                int leftCount = 0;//6
                int rightCount = 0;//1
                for (Integer value : map.values()) {
                    if (value == left) {
                        leftCount++;
                    } else {
                        rightCount++;
                    }
                }


            }
        }

        return res;
    }


}

